package com.zmn.plat.admin.controller.message.config;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.KvModel;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.admin.controller.message.MsgTemplateBaseController;
import com.zmn.plat.common.dictionary.ChannelTypeEnum;
import com.zmn.plat.manager.constant.MsgTemplateConsts;
import com.zmn.plat.model.entity.message.MsgTemplate;
import com.zmn.plat.model.entity.message.MsgTemplateChannelQuery;
import com.zmn.plat.model.entity.message.MsgTemplateChannelVO;
import com.zmn.plat.model.entity.message.MsgTemplateConfigVO;
import com.zmn.plat.services.interfaces.area.BaseAreaService;
import com.zmn.plat.services.interfaces.channel.ChannelService;

/**
 * 描述: 消息管理配置查询Controller
 *
 * @auther tanghongjun
 * @since 2019/10/21 16:36
 */
@Controller
@RequestMapping("/msg/temp/conf/")
public class MsgTemplateConfigListController extends MsgTemplateBaseController {

    @Resource
    private BaseAreaService baseAreaService;

    @Resource
    private ChannelService channelService;

    /**
     * 服务区域 城市配置页面
     *
     * @param templateId
     * @return
     */
    @RequiresPermissions("/msg/temp/detail.action")
    @GetMapping("configCityByTemplateId")
    public ModelAndView configCity(@RequestParam(value = "templateId", required = true) Integer templateId) {
        ModelAndView mav = new ModelAndView("message/config/config-city");
        List<KvModel<Integer, String>> allCities = baseAreaService.listAllBaseArea();
        mav.addObject("allCities", allCities);
        mav.addObject("templateId", templateId);
        MsgTemplate obj = msgTemplateService.findByKey(templateId);
        mav.addObject("obj", obj);
        mav.addObject("flag", 4);

        List<MsgTemplateConfigVO> msgTemplateConfigs =
            msgTemplateConfigService.listByTemplateIdAndType(templateId, MsgTemplateConsts.MSG_TEMP_TYPE_CITY);
        List<KvModel<Integer, String>> selectedCities = msgTemplateConfigs.stream()
            .map(msgTemplateConfigVO -> new KvModel<>(msgTemplateConfigVO.getBusinessId(), msgTemplateConfigVO.getBusinessName()))
            .collect(Collectors.toList());

        mav.addObject("selectedCities", selectedCities);

        return mav;
    }

    /**
     * 渠道配置页面
     *
     * @param templateId
     * @param query
     * @return the model and view
     * @author xiangjunming
     * @since 2019/10/29
     */
    @RequestMapping(value = "configChannel")
    @RequiresPermissions("/msg/temp/detail.action")
    @ReadOnlyConnection
    public ModelAndView configChannel(Integer templateId, MsgTemplateChannelQuery query) {
        ModelAndView mav = new ModelAndView("message/config/channel/list");
        MsgTemplate obj = msgTemplateService.findByKey(templateId);

        List<MsgTemplateChannelVO> items = msgTemplateConfigService.listPageChannelByQuery(query);

        mav.addObject("items", items);
        mav.addObject("plats", GlobalConsts.getPlatList());
        mav.addObject("channelTypes", ChannelTypeEnum.enables());
        mav.addObject("query", query);
        mav.addObject("obj", obj);
        mav.addObject("flag", 3);
        return mav;
    }

    /**
     * 选择渠道页面
     *
     * @param templateId
     *            模板ID
     * @return the model and view
     * @author xiangjunming
     * @since 2019/10/29
     */
    @RequestMapping(value = "selectChannel")
    // @RequiresPermissions("/msg/temp/conf/selectChannel.action")
    @ReadOnlyConnection
    public ModelAndView selectChannel(Integer templateId) {
        ModelAndView mav =
            new ModelAndView("forward:/channel/select/list.action?commitURL=/msg/temp/conf/addChannelConfig&id=" + templateId);
        // // 选中渠道提交地址
        // ChannelSelectCommitVO commitVO = new ChannelSelectCommitVO();
        // commitVO.setCommitURL("/msg/temp/conf/addChannelConfig");
        // commitVO.setId(templateId);
        // mav.addObject("commitVO", commitVO);
        return mav;
    }

}
